﻿@model ViVa.ActiveDirectory.ADEntry

<script language="javascript" type="text/javascript">
    

        $('#searchbar_voeggroeptoe').keyup(function (e) {
            if (e.keyCode == 13) {
                
                laatZien(document.getElementById('searchbar_voeggroeptoe').value); 
            }
        });

   

    $(document).ready(function () {

        var text = "Geef zoekterm...";

        $("#searchbar_voeggroeptoe").focus(function () {
            $(this).addClass("active");
            if ($(this).attr("value") == text) $(this).attr("value", "");
        });

        $("#searchbar_voeggroeptoe").blur(function () {
            $(this).removeClass("active");
            if ($(this).attr("value") == "") $(this).attr("value", text);
        });
    });

    function laatZien(searchText) {

        $('#Loading')
    .hide()  // hide it initially
    .ajaxStart(function () {
        $(this).show();
    })
    .ajaxStop(function () {
        $(this).hide();
    });

        $("#Loading").empty().html('<img src="/Content/Images/load.gif" />');
        var url = '@Url.Action("getGroupsJSON", "ADUser")';
        var url = url + "?groupString=" + searchText;
        var samAccountNameHTML = '@Html.HiddenFor(x => x.SamAccountName)';

        
        
        $.ajax({ type: "POST", // jQuery ajax with POST method
            url: url,
            success: function (serverResponseData) // On success call function
            {
                dtItems = eval("(" + serverResponseData + ")"); // evaluate retrived data to javascript object
                var htmlGrid = "<input type=\"hidden\" name=\"action\" value=\"AddMemberOf\" />";
                htmlGrid += "<form method=\"post\">";
                htmlGrid += "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" class=\"grid\">";

                for (var i = 0; i < dtItems.length; i++) // traverse through items in this object
                {
                    var rowClass = (i % 2 == 0) ? "gridRow" : "gridRowAlternate";
                    htmlGrid += "<tr class=\"" + rowClass + "\"><td> <input type=\"checkbox\" name=\"" + dtItems[i].DistinguishedName + "\"></td><td>" + dtItems[i].Name + "</td></tr>";
                    // Build grid from retrived data in current item
                }
                htmlGrid += samAccountNameHTML;
                htmlGrid += "<input type=\"hidden\" name=\"action\" value=\"AddMemberOf\" />";
                htmlGrid += "<tr><td><input type=\"submit\" value=\"save\" /></td></tr>";
                htmlGrid += "</table></form>";

                $("#divJSONGrid").html(htmlGrid); // apply created grid HTML to a div "divJSONGrid"
            }

        });
    }
</script>


        @Html.TextBox("searchbar_voeggroeptoe", "Geef zoekterm...", new { @autocomplete = "off", @class = "searchbar-big"}) <button id="searchbtn" type="submit" onclick = "laatZien(document.getElementById('searchbar_voeggroeptoe').value);" > Zoek groep... </button> <span>&nbsp</span> <label id="Loading"> </label>
             
  <br /><br />                                                                                                                                  
<h2>Groepen:</h2>
<div id="posts"> </div>

<div id="divJSONGrid"> </div>

